home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2109 / 2109.xpi / chrome / febe.jar / content / febeZip.js < prev   
Text File  |  2009-10-13  |  8KB  |  248 lines

  1. // author: Chuck Baker
  2. // contact: febe@customsoftwareconsult.com
  3. // Version 6.3
  4.  
  5. function febeZip(sourceDir,destDir,zipName,absolutePath,append){
  6.   // Zip the contents of sourceDir (including all subdirectories) into destDir as zipName
  7.   // If absolutePath is true, include the full pathname for each entry added to zipName
  8.   // If append is false, create a new zipfile, otherwise append to the existing zipfile
  9.   // Initialize
  10.     var root = src = sourceDir;
  11.     var zipW;
  12.     
  13.     const PR_RDONLY      = 0x01
  14.     const PR_WRONLY      = 0x02
  15.     const PR_RDWR        = 0x04
  16.     const PR_CREATE_FILE = 0x08
  17.     const PR_APPEND      = 0x10
  18.     const PR_TRUNCATE    = 0x20
  19.     const PR_SYNC        = 0x40
  20.     const PR_EXCL        = 0x80
  21.     const COMPRESSION_NONE    = 0;
  22.     const COMPRESSION_FASTEST = 1;
  23.     const COMPRESSION_DEFAULT = 6;
  24.     const COMPRESSION_BEST    = 9;
  25.     
  26.     var ZipWriter = Components.Constructor("@mozilla.org/zipwriter;1","nsIZipWriter");
  27.     var ZipReader = Components.Constructor("@mozilla.org/libjar/zip-reader;1","nsIZipReader", "open");
  28.  
  29.     // Init routine
  30.     var init = function (destDir,zipName){
  31.         febeZipFinished = false;    // Set to 'true' when zipping is completed
  32.         var observerService = new febeObserver();
  33.         observerService.notify(null,"buItem",zipName);
  34.  
  35.         var zipFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  36.         zipFile.initWithPath(destDir);
  37.         if(!zipFile.exists() || !zipFile.isDirectory()){
  38.             zipFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  39.         }//if        
  40.         zipFile.append(zipName);
  41.         if (zipFile.exists() && !append){zipFile.remove(true);}
  42.         zipW = new ZipWriter();
  43.         if(append){
  44.             zipW.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_APPEND);
  45.         }else{
  46.             zipW.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
  47.         }
  48.     }//init()
  49.     
  50.     // Define the zip routine
  51.     var zip = function (src){
  52.         var aFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  53.         if (!aFile) return false;
  54.         aFile.initWithPath(src);
  55.         if(!aFile.exists()){
  56.             var tmp = "febeZip: "+febeMsg[61].replace(/%SRC%/,src);
  57.             febeFatal("FILE NOT FOUND",tmp);
  58.             return talse;
  59.         }//if
  60.         
  61.         var entries = aFile.directoryEntries;
  62.         while(entries.hasMoreElements()){
  63.             var entry = entries.getNext();
  64.             entry.QueryInterface(Ci.nsIFile);
  65.             var src = entry.path;
  66.             if(entry.isDirectory()){
  67.                 zip(src);    //Recurse!
  68.             }else{
  69.                 var file = src.substring(root.length+1);;
  70.                 if(absolutePath) file = src;
  71.                 file = file.replace(/\\/g,"/");
  72.                 
  73.                 // Ignore locked files
  74.                 if(file == "parent.lock"){continue;}
  75.                 if(file == "lock"){continue;}
  76.                 //if(file == "places.sqlite-journal"){continue;}
  77.                 if(file == "places.sqlite-stmtjrnl"){continue;}
  78.                 //if(file == "places.sqlite-journal"){continue;}
  79.                 //if(file == "cookies.sqlite-journal"){continue;}
  80.                 var jrnl = file.lastIndexOf(".sqlite-journal");
  81.                 if(jrnl > 0 && (file.length - jrnl == 15)){continue;}    // Ignore everything that ends with ".sqlite-journal"
  82.                 try{
  83.                     zipW.addEntryFile(file, Ci.nsIZipWriter.COMPRESSION_BEST, entry, false);
  84.                 }catch(e){
  85.                     febeSetMsgs();
  86.                     if(e.toString().indexOf("NS_BASE_STREAM_CLOSED") != -1){continue;}
  87.                     if(e.toString().indexOf("NS_ERROR_FILE_ALREADY_EXISTS") != -1){
  88.                         zipW.removeEntry(file, false);
  89.                         zipW.addEntryFile(file, Ci.nsIZipWriter.COMPRESSION_BEST, entry, false);
  90.                         continue;
  91.                     }//if
  92.                     if(e.toString().indexOf("NS_ERROR_NOT_INITIALIZED") != -1){
  93.                         var style = "<style>color: red;font-size: 10pt;</style>";
  94.                         var tmp = style+febeMsg[276]+"\n\n";
  95.                         style = "<style>color: black;font-size: 8pt;</style>";
  96.                         tmp += style+src+"\n\n"
  97.                         tmp += febeMsg[277];
  98.                         if(febeConfirm(tmp)){continue;}
  99.                         setTimeout("febeCloseProgressWindow()",500);
  100.                     }//if
  101.  
  102.                     var msg = febeMsg[53].replace(/%FILE%/,file);
  103.                     febeFatal(e,msg);
  104.                     return false;
  105.                 }//try/catch
  106.             }//if
  107.         }//while
  108.     }//zip()
  109.     
  110.     // nsIRequestObserver methods
  111.     var observer = {
  112.         onStartRequest: function(request, context){},
  113.         onStopRequest:  function(request, context, status){
  114.             zipW.close();
  115.             febeZipFinished = true;
  116.         }      
  117.     };
  118.     init(destDir,zipName);
  119.     zip(src);
  120.     zipW.processQueue(observer,null);
  121.     return true;
  122. }// febeZip()
  123.  
  124. function febeUnzip(sourceDir,destDir,zipName){
  125.   // Unzip the contents of zipName (located in sourceDir) into destDir overwriting existing entries
  126.     var zipFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  127.     zipFile.initWithPath(sourceDir);
  128.     zipFile.append(zipName);
  129.     var zipR = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(Ci.nsIZipReader);
  130.     zipR.open(zipFile);
  131.     var entries = zipR.findEntries("*");
  132.     while (entries.hasMore()) {
  133.         var entry = entries.getNext();
  134.         var zEntry = zipR.getEntry(entry)
  135.         var outFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  136.         outFile.initWithPath(destDir);
  137.         var subDirs = entry.split("/");
  138.         var end = subDirs.length;
  139.         if(zEntry.isDirectory){--end;}
  140.         
  141.         for (var i = 0; i < end; ++i) {
  142.           if(!febeIsDrive(subDirs[i])) outFile.append(subDirs[i]);
  143.           if (!(outFile.exists() || febeIsDrive(subDirs[i]))) {
  144.                 try{
  145.                     outFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  146.                     //throw("error 123")
  147.                 }catch(e){
  148.                     var style = "<style>color: red;font-size: 9pt;</style>";
  149.                     var tmp = style+e.toString()+"\n\n";
  150.                     var style = "<style>color: black;font-size: 9pt;</style>";
  151.                     tmp += style+"sourceDir: "+sourceDir+"\n\n";
  152.                     tmp += style+"destDir: "+destDir+"\n\n";
  153.                     tmp += style+"zipName: "+zipName+"\n\n";
  154.                     tmp += style+"outFile: "+outFile.path+"\n\n";
  155.                     tmp += style+"length: "+outFile.path.length+"\n\n";
  156.                     
  157.                     febeAlert(tmp);
  158.                     return false;
  159.                 }
  160.             }
  161.         }
  162.  
  163.         if(!zEntry.isDirectory){
  164.           outFile.append(subDirs[end]);
  165.           zipR.extract(entry,outFile);
  166.         }
  167.     }//while
  168.     return true;
  169.   }//febeUnzip()
  170.   
  171. function febeIsDrive(path){
  172.     // Is path a drive disignator?  ("C:", etc)
  173.     if(path.match(":")) return true
  174.     return false;
  175. }//febeIsDrive()
  176.   
  177.   function febeAddToZip(sourceNames,destDir,zipName){
  178.   // Zip the files listed in sourceNames array into destDir as zipName -  sourceNames contains full pathnames of files only.  Directories are ignored, zipName is overwriten
  179.     // Initialize
  180.     var zipW;
  181.     
  182.     const PR_RDONLY      = 0x01
  183.     const PR_WRONLY      = 0x02
  184.     const PR_RDWR        = 0x04
  185.     const PR_CREATE_FILE = 0x08
  186.     const PR_APPEND      = 0x10
  187.     const PR_TRUNCATE    = 0x20
  188.     const PR_SYNC        = 0x40
  189.     const PR_EXCL        = 0x80
  190.     const COMPRESSION_NONE    = 0;
  191.     const COMPRESSION_FASTEST = 1;
  192.     const COMPRESSION_DEFAULT = 6;
  193.     const COMPRESSION_BEST    = 9;
  194.     
  195.     var ZipWriter = Components.Constructor("@mozilla.org/zipwriter;1",
  196.                                        "nsIZipWriter");
  197.     var ZipReader = Components.Constructor("@mozilla.org/libjar/zip-reader;1",
  198.                                        "nsIZipReader", "open");
  199.  
  200.     // Init routine
  201.     var init = function (destDir,zipName){
  202.         febeZipFinished = false;    // Set to 'true' when zipping is completed
  203.         var zipFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  204.         zipFile.initWithPath(destDir);
  205.         if(!zipFile.exists() || !zipFile.isDirectory()){
  206.             zipFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  207.         }//if        
  208.         zipFile.append(zipName);
  209.         if (zipFile.exists()){zipFile.remove(true);}
  210.         zipW = new ZipWriter();
  211.         zipW.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
  212.     }//init()
  213.     
  214.     // Define the zip routine
  215.     var zip = function (){
  216.         for(var i in sourceNames){
  217.             var aFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  218.             if (!aFile) return false;
  219.             var entry = sourceNames[i];
  220.             aFile.initWithPath(entry.path);
  221.             aFile.append(entry.name);
  222.             if(aFile.isDirectory()){
  223.                 continue;
  224.             }else{
  225.                 try{
  226.                     zipW.addEntryFile(entry.name, Ci.nsIZipWriter.COMPRESSION_BEST, aFile, false);
  227.                 }catch(e){
  228.                     var msg = febeMsg[53].replace(/%FILE%/,entry.name)
  229.                     febeFatal(e,msg)
  230.                 }
  231.  
  232.             }//if
  233.         }//for
  234.     }//zip()
  235.     
  236.     // nsIRequestObserver methods
  237.     var observer = {
  238.         onStartRequest: function(request, context){},
  239.         onStopRequest:  function(request, context, status){
  240.             zipW.close();
  241.             febeZipFinished = true;
  242.         }      
  243.     };
  244.     init(destDir,zipName);
  245.     zip();
  246.     zipW.processQueue(observer,null);
  247.     return true;
  248. }// febeAddToZip()